<template>
  <article class="page">
    <headerbar>筑红云联盟</headerbar>
    <section class="frame-content-sub no-bg"  v-remainScroll="'otherPlatform'">

      <section class="self-height">
        <section class="people-list list-block mgt-2">


          <div @click="detail(item)" class="people-item" v-for="(item,index) in list" :key="index">
            <section class="people-info">
              <div class="avatar-img">
                <img v-if="item.pic" :src="tools.getImgUrl(item.pic)" alt="">
                <img v-if="!item.pic" src="../../../../static/images/dangjian/home/img-load-error.jpg" alt="">
              </div>
              <span class="name">{{item.channelName}}</span>
            </section>
            <span v-if="item.beyond === '0'" @click.stop="like(item.id)" class="btn">+ 关注</span>
            <span v-if="item.beyond === '1'" @click.stop="submitDelete(item.id)" class="btn">- 取消关注</span>
          </div>


        </section>
      </section>

    </section>
  </article>
</template>


<script>
  export default {
    name: 'mySubscriptions',
    data () {
      return {
        list: [],
        likeList: []
      }
    },
    created () {
      this.fetchPartyList()
      // this.userDept()
    },
    methods: {
      detail (item) {
        this.$store.commit('addRoute','newsChannel')
        this.$store.state.newsChanelData = item
        this.$router.push({name: 'newsChannel', query: {id: item.id, channelName: item.channelName}})
      },
      isLike (deptId) {
        let ret = false
        this.likeList.some(v => {
          if (v.deptId === deptId) {
            return ret = true
          }
        })
        return ret
      },
      like (deptId) {
        this.fetchService.addUserChannelDeptOne(deptId).then((res) => {
          if (res.code !== 0) {
            return this.tools.tip(res.message)
          } else {
            this.likeList.push({deptId})
            this.fetchPartyList()
            return this.tools.tip('添加关注成功', 2000)
          }
        })
      },
      fetchPartyList () {
        let params = {
          id: this.tools.getUserInfo().id,
          publicWay: '1',
          page: 1,
          limit: 100
        }
        this.fetchService.currentDeptUserPage(params).then(res => {
          if (res.code !== 0) {
            return this.tools.alert(res.message || res.resp_msg)
          }
          this.list = res.data
        })
      },
      userDept () {
        this.fetchService.userDept().then(res => {
          if (res.code !== 0) {
            return this.tools.alert(res.message || res.resp_msg)
          }
          this.likeList = res.data
        })
      },
      submitDelete (deptId) {
        this.fetchService.userChannelDeptOne(deptId).then((res) => {
          if (res.code !== 0) {
            return this.tools.tip(res.message)
          } else {
            this.tools.tip('取消关注成功', 2000)
            this.fetchPartyList()
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
